#include "webwnd.h"
#include "ui_webwnd.h"

//////////////////////////////////////////////////////////////////////////
//
WebWnd::WebWnd(const QString & strUrl, QWidget * parent, Qt::WindowFlags flags)
    : ExtendWidget(parent, flags)
    , m_view(nullptr)
    , m_bStop(true)
    , m_strUrl(strUrl)
    , ui(new Ui::WebWnd)
{
    ui->setupUi(this);

    //
    init();
}

WebWnd::~WebWnd()
{
    fini();

    //
    delete ui;
}

void WebWnd::fini()
{
    disconnect(m_view, nullptr, nullptr, nullptr);

    //
    if (m_view != nullptr)
    {
        delete m_view;
        m_view = nullptr;
    }
}

void WebWnd::init()
{
    m_defaultIcon = QIcon(":/pic/browser.svg");
    m_errorIcon = QIcon(":/pic/error.svg");
    m_stopIcon = QIcon(":/pic/stop.svg");
    m_reloadIcon = QIcon(":/pic/reload.svg");

    m_loadingIcon1 = QIcon(":/pic/loading1.png");
    m_loadingIcon2 = QIcon(":/pic/loading2.png");
    m_loadingIcon3 = QIcon(":/pic/loading3.png");
    m_loadingIcon4 = QIcon(":/pic/loading4.png");

    //
    ui->btBack->setEnabled(true);
    ui->btForward->setEnabled(true);

    connect(ui->btBack, &QToolButton::clicked, this, &WebWnd::onBack);
    connect(ui->btForward, &QToolButton::clicked, this, &WebWnd::onForward);
    connect(ui->btReload, &QToolButton::clicked, this, &WebWnd::onReload);
    connect(ui->btHome, &QToolButton::clicked, this, &WebWnd::onHome);
    connect(ui->leUrl, &QLineEdit::returnPressed, this, &WebWnd::onLoadUrl);
}

void WebWnd::initWeb()
{
    //
    if (m_view == nullptr)
    {
        m_view = new WebView(m_strUrl, this);
        Q_ASSERT(m_view);
    }

    connect(m_view, &WebView::newWebView, this, &WebWnd::newWebWnd);
    connect(m_view, &WebView::newMainWnd, this, &WebWnd::newMainWnd);

    connect(m_view, &WebView::pageAfterCreated, this, &WebWnd::onPageCreated);
    connect(m_view, &WebView::pageAddressChange, this, &WebWnd::onAddressChanged);
    connect(m_view, &WebView::pageFaviconChange, this, &WebWnd::onFaviconChanged);
    connect(m_view, &WebView::pageLoadStart, this, &WebWnd::onLoadStarted);
    connect(m_view, &WebView::pageLoadingProgressChange, this, &WebWnd::onLoadingProgressChanged);
    connect(m_view, &WebView::pageTitleChange, this, &WebWnd::titleChange);

    ui->layWebView->addWidget(m_view);
    ui->layWebView->removeItem(ui->spWebView);
}


void WebWnd::closeEvent(QCloseEvent *event)
{
    //
    if (m_view != nullptr)
    {
        m_view->close();
    }

    //
    __super::closeEvent(event);
}

void WebWnd::firstShowedEvent(QEvent * event)
{
    //
    initWeb();
}


QSize WebWnd::sizeHint() const
{
    return QSize(300, 300);
}

void WebWnd::onPageCreated()
{
    if (m_strUrl.length() <= 0)
    {
        QTimer::singleShot(500, this, [this](){
            this->setNaviFocus(true);
        });
    }
}

QLineEdit * WebWnd::getUrlWidget()
{
    return ui->leUrl;
}

void WebWnd::back()
{
    if (m_view != nullptr)
    {
        m_view->goBack();
        m_view->setBrowserFocus();
    }
}

void WebWnd::forward()
{
    if (m_view != nullptr)
    {
        m_view->goForward();
        m_view->setBrowserFocus();
    }
}

void WebWnd::reload()
{
    if (m_view != nullptr)
    {
        m_view->reload();
        m_view->setBrowserFocus();
    }
}

void WebWnd::stop()
{
    if (m_view != nullptr)
    {
        m_view->stopLoad();
        m_view->setBrowserFocus();
    }
}

void WebWnd::load(const QUrl & url)
{
    return loadUrl(url.url());
}

void WebWnd::loadUrl(const QUrl & url)
{
    if (m_view != nullptr)
    {
        m_view->loadUrl(url.url());
        m_view->setBrowserFocus();
    }
}

void WebWnd::setUrl(const QString & val)
{
    if (m_view != nullptr)
    {
        m_view->setUrl(val);
        m_view->setBrowserFocus();
    }
}

QString WebWnd::getUrl()
{
    if (m_view == nullptr)
    {
        return "";
    }

    return m_view->getUrl();
}

void WebWnd::setBrowserFocus(bool focus)
{
    if (m_view != nullptr)
    {
        m_view->setBrowserFocus(focus);
    }
}

void WebWnd::setNaviFocus(bool focus)
{
    if (focus)
    {
        ui->leUrl->setFocus();
    }
}


void WebWnd::onLoadUrl()
{
    if (m_view != nullptr)
    {
        QUrl url = QUrl::fromUserInput(ui->leUrl->text());

        m_view->loadUrl(url.url());
        m_view->setBrowserFocus();
    }
}


void WebWnd::onAddressChanged(QString url)
{
    ui->leUrl->setText(QUrl::fromUserInput(url).toDisplayString());
}


void WebWnd::onFaviconChanged(QString strImageUrl, int http_status_code, QIcon icon)
{
    emit loadIconChange(icon);
}


void WebWnd::onLoadingProgressChanged(double progress)
{
    int percent = progress * 100;

    if (percent > 0 && percent < 100)
    {
        switch (percent % 4)
        {
        case 0:
            emit loadIconChange(m_loadingIcon1);
            break;
        case 1:
            emit loadIconChange(m_loadingIcon2);
            break;
        case 2:
            emit loadIconChange(m_loadingIcon3);
            break;
        case 3:
            emit loadIconChange(m_loadingIcon4);
            break;
        default:
            emit loadIconChange(m_loadingIcon1);
            break;
        }
    }
    else
    {
        emit loadIconChange(m_defaultIcon);
    }

    //
    updateReloadButton(percent);
}

void WebWnd::onLoadStarted(int transition_type)
{

}

void WebWnd::onWebLinkHovered(QString url)
{
    ui->lbInfo->setText(QUrl::fromUserInput(url).toDisplayString());
}

void WebWnd::updateReloadButton(int progress)
{
    if (progress > 0 && progress < 100)
    {
        m_bStop = false;
        ui->btReload->setIcon(m_stopIcon);
        ui->btReload->setToolTip("停止");
        ui->btReload->setStatusTip("停止");
    }
    else
    {
        m_bStop = true;
        ui->btReload->setIcon(m_reloadIcon);
        ui->btReload->setToolTip("刷新");
        ui->btReload->setStatusTip("刷新");
    }
}


void WebWnd::onBack()
{
    this->back();
}


void WebWnd::onForward()
{
    this->forward();
}


void WebWnd::onReload()
{
    if (m_bStop)
    {
        this->reload();
    }
    else
    {
        this->stop();
    }
}


void WebWnd::onHome()
{
    loadUrl(QUrl::fromUserInput("https://www.baidu.com"));
}


void WebWnd::onLoadEnded(int httpStatusCode)
{
    //...
}

